<?php

namespace app\model;

use think\facade\Db;

class DriverClockModel
{
    public static function lists($param)
    {
        $where[] = [
            'clock.driver_id', '=', $param['driver_id'],
        ];
        if ($param['stime'] && $param['etime']) {
            $where[] = ['clock.date', 'between', [$param['stime'], $param['etime']]];
        }
        if ($param['is_export'] == 1) {
            $param['limit'] = Db::table('xl_driver_clock')->alias('clock')->where($where)->count();
        }
        $list = Db::table('xl_driver_clock')->alias('clock')->where("clock.status", "=", 1)
            ->join('xl_cars cars', 'clock.car_id = cars.id', 'left')
            ->where($where)
            ->limit(($param['page'] - 1) * $param['limit'], $param['limit'])
            ->field('cars.car_plate_number,clock.*')
            ->order('id desc')
            ->select()->toArray();
        $count = Db::table('xl_driver_clock')->alias('clock')->where($where)
            ->count();
        return ['list' => $list, 'count' => $count];
    }

    /**
     * 新增司机信息
     * @return void
     */
    public static function add($data)
    {
        return Db::table('xl_driver_clock')->insert($data);
    }

    /**
     * 编辑司机信息
     * @return void
     */
    public static function edit($data, $id)
    {
        return Db::table('xl_driver_clock')->where('id', '=', $id)->update($data);
    }

    /**
     * 删除司机信息
     * @return void
     */
    public static function del($id)
    {
        return Db::table('xl_driver_clock')->where('id', '=', $id)->update(['status' => 3]);
    }

    /**
     * 获取司机信息
     * @return void
     */
    public static function info($id)
    {
        return Db::table('xl_driver_clock')->where('id', '=', $id)->field('id,name')->find();
    }
}
